package com.qf.chinatelcom.smsatragety.stragety;

import com.qf.chinatelcom.smsatragety.dfa.SensitivewordFilter;
import com.qf.chinatelcom.smsatragety.filter.DuTyFilter;
import com.qianfeng.smsplatform.common.constants.InterfaceExceptionDict;
import com.qianfeng.smsplatform.common.model.Standard_Submit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class DirtyWordsFilter implements DuTyFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(DirtyWordsFilter.class);

    private SensitivewordFilter sensitivewordFilter;

    @Autowired
    public void setSensitivewordFilter(SensitivewordFilter sensitivewordFilter) {
        this.sensitivewordFilter = sensitivewordFilter;
    }

    @Override
    public boolean dealSms(Standard_Submit submit) {
        System.err.println("敏感词开始处理了");
        String messageContent = submit.getMessageContent();//获取到短信内容
        Set<String> sensitiveWord = sensitivewordFilter.getSensitiveWord(messageContent);//看看内容中是否有敏感词，有的话就返回，这个效率比循环效率快的快的多
        if (sensitiveWord != null && sensitiveWord.size() > 0) {
            LOGGER.error("发现敏感词:{}", sensitiveWord);
            submit.setDescription("短信内容中出现了敏感词:"+sensitiveWord);
            submit.setErrorCode(InterfaceExceptionDict.RETURN_STATUS_DIRTY_ERROR+"");//失败的原因
            submit.setReportState(2);//失败的标识为
            return false;//发现敏感词并拦截
        }
        return true;
    }
}
